<?php

namespace Yxchat\Controller;

//use Think\Controller\RestController;
use Common\Controller\ApiController;
use Yxchat\Model\CallbackNoticeModel;
use Ruben\Yxyun\Yxim;
use Think\Log;

class CallbackNoticeController extends ApiController {

    private $body;

    public function _initialize() {
        header('Access-Control-Allow-Origin:*');
        $headers = getallheaders();
        $imConfig = C('sdk.yxim');
        $yxim = new Yxim($imConfig);
        $yxim->checkSumBuilder();
        //本来应该检验一下请求数据的真实性的，过过过
        //{"Content-Type":"application\/json;charset=UTF-8","Content-Length":"319","Appkey":"d7321059adf7bf055220344fcb6edb21","Curtime":"1578130078395","Md5":"2e42b6f3c832be037e67c9954a1656be","Checksum":"d13e03cf3e6d0ca9b3504a8e4437eb7225773de9","Host":"app.52jieban.com","Connection":"Keep-Alive","Accept-Encoding":"gzip","User-Agent":"okhttp\/3.11.0"}
//        $checksum = sha1($imConfig['App_secret'].$headers['Md5'].$headers['Curtime']);
        $body = file_get_contents('php://input');
        Log::record(json_encode($body), 'info', true);
        $this->body = json_decode($body,true);
    }

    public function index() {
        $body = $this->body;
        $num = 0;
        switch ($body['eventType']) {
            case 4://添加好友回调
                    $num = $this->addFriend($body);
                break;
            case 5://删除好友回调
                $num =$this->delFriend($body);
                break;
        }
        Log::record($num, '--return--', true);
        \Think\Controller\RestController::response(['errCode' => 0],'json');
    }

    public function addFriend($body) {
        //添加关注
      return   CallbackNoticeModel::addFriend($body);
        
    }
    public function delFriend($body) {
        //添加关注
       return CallbackNoticeModel::delFriend($body);
    }

}
